//
//  FirstController.m
//  QQ inteface
//
//  Created by dllo on 16/6/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FirstController.h"
#import "Firstview.h"
#import "Contact.h"
#import "AddressBook.h"
@interface FirstController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain)AddressBook *addressBook;


@end

@implementation FirstController
-(void)dealloc{
    [_addressBook release];
    [super dealloc];
    
}
- (void)viewDidLoad {
    self.addressBook = [[AddressBook alloc]init];
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHue:15 saturation:10 brightness:0 alpha:0.3];
    [self congtou];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self shuju];
    
self.navigationItem.title = @"第一页";
    
}

-(void)congtou{
    UISegmentedControl *seg =[[ UISegmentedControl alloc]initWithItems:@[@"消息",@"电话"]];
    seg.frame = CGRectMake(100, 10, 150, 30);
     self.navigationItem.titleView = seg;
    seg.selectedSegmentIndex = 0;
        [seg release];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"数值加.png"] style:UIBarButtonItemStylePlain target:self action:@selector(numberAction:)];
    
    UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
    tableview .backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tableview];
    
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.rowHeight = 100;

    [tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    
    
    
}
-(void)shuju{
    
    NSString *str = [[NSBundle mainBundle]pathForResource:@"Pro" ofType:@".plist"];
  
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:str];
   
    NSArray *array = [dic.allKeys sortedArrayUsingSelector:@selector(compare:)];
    


    
    
    
    for (NSString *key in array) {
        NSArray *newarray = [dic objectForKey:key];
        
        NSMutableArray *muarray = [NSMutableArray arrayWithCapacity:5];
        
        for (NSDictionary *dict in newarray) {
            Contact *contact = [[Contact alloc]init];
            [contact setValuesForKeysWithDictionary:dict];
            [muarray addObject:contact];
            
        }
        [self.addressBook.dictonary setObject:muarray forKey:key];
        
    }
     NSLog(@"addressBook:%@",self.addressBook.dictonary);
    

    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSArray *groupNames = [self.addressBook getGroupNames];
    
    NSString *key = [groupNames objectAtIndex:section];
    /*再根据key取出对应的联系人分组.*/
    NSArray *array = [self.addressBook.dictonary objectForKey:key];
    
    return array.count;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.addressBook.dictonary.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSArray *groupNames  = [self.addressBook getGroupNames];
    NSArray *contactArry = [self.addressBook.dictonary objectForKey :[groupNames objectAtIndex:indexPath.section]];
    Contact *contact =[contactArry objectAtIndex:indexPath.row];
    cell.textLabel.text  = contact.name;
    
    cell.imageView.image = [self reSizeImage:[UIImage imageNamed:contact.poto] andSize:CGSizeMake(tableView.rowHeight, tableView.rowHeight)];
    
    cell.imageView.layer.cornerRadius = tableView.rowHeight/2;
    
    cell.imageView.layer.masksToBounds = YES;
    
    return  cell;
    
}
- (UIImage *) reSizeImage:(UIImage *)image andSize:(CGSize)reSize {
    
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width,reSize.height));
    
    [image drawInRect:CGRectMake(0, 0, reSize.width,reSize.height)];
    
    UIImage *reSizeImage =UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return reSizeImage;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return  [[self.addressBook getGroupNames ]objectAtIndex:section];
    
}
-(NSArray<NSString*>*)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return  [self.addressBook getGroupNames];
}
-(void)numberAction:(UIButton*)button{
    Firstview *first = [[Firstview alloc]initWithFrame:CGRectMake(0, 80, 500,100) withImage:
                        [UIImage imageNamed:@"6_1.jpg"]withText:@"野马跑车" ];
    
    [self.view addSubview:first];
    [first release];
}





@end
